package cn.picclife.mwx.manager.sys.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.picclife.mwx.manager.base.BaseResult1;
import cn.picclife.mwx.manager.sys.model.bo.UserSessionBo;
import cn.picclife.mwx.manager.sys.model.po.PanelPoint;
import cn.picclife.mwx.manager.sys.service.FlowPathService;
import cn.picclife.mwx.manager.sys.service.PanelPointService;
import cn.picclife.mwx.manager.util.RestUtils;

@Controller
@RequestMapping("/api")
public class PanelPointController {
	
	@Autowired
	private PanelPointService service;
	
	@Autowired
	private FlowPathService flowPathService;
	
	@RequestMapping(value = "/panelpoint/add_panelpoint")
	@ResponseBody
	public BaseResult1 insertPanelPoint( PanelPoint panelPoint,HttpServletRequest request,HttpServletResponse response) throws Exception {
		BaseResult1 result = new BaseResult1();
		//超过节点个数   提示不能添加
		if (StringUtils.isBlank(panelPoint.getName())) {
			result.setRet(BaseResult1.BIZ_ERROR);
			result.setMsg("节点名称不能为空!");
			RestUtils.error(response, result);
			return result;
		}
		List<PanelPoint>list = service.queryPanelPoints(panelPoint.getFlowpath_Id());
		for (PanelPoint panelPoint2 : list) {
			if (panelPoint.getName().equals(panelPoint2.getName())) {
				result.setRet(BaseResult1.BIZ_ERROR);
				result.setMsg("节点名称已经存在!");
				RestUtils.error(response, result);
				return result;
			}
		}
		if ("".equals(panelPoint.getOperate())) {
			result.setRet(BaseResult1.BIZ_ERROR);
			result.setMsg("双录节点操作不能为空!");
			RestUtils.error(response, result);
			return result;
		}
		if (StringUtils.isBlank(panelPoint.getWarn())) {
			result.setRet(BaseResult1.BIZ_ERROR);
			result.setMsg("双录提示语不能为空!");
			RestUtils.error(response, result);
			return result;
		}
		 UserSessionBo user = (UserSessionBo) request.getSession().getAttribute("user");
		 panelPoint.setCreateUser(user.getOptName());
		int flag = service.insertPanelPoint(panelPoint);
		if (flag >0) {
			result.setRet(BaseResult1.SUCCESS);
			result.setMsg("保存成功!");
			return result;
		}else{
			result.setRet(BaseResult1.SUCCESS);
			result.setMsg("保存失败!");
			return result;
		}
	}
	
	@RequestMapping(value = "/panelpoint/query_panelpointById")
	@ResponseBody
	public PanelPoint queryPanelPointById(int panelpoint_id ,HttpServletRequest request,HttpServletResponse response){
		return service.queryPanelPointByPanelId(panelpoint_id);
	}
	
	@RequestMapping(value = "/panelpoint/query_panelpoint")
	@ResponseBody
	public Map<String, Object>queryPanelPoint(int flowPath_id ,HttpServletRequest request,HttpServletResponse response){
		List<PanelPoint>list = service.queryPanelPoints(flowPath_id);
		Map<String, Object>tempMap= flowPathService.searchFlowPathById(flowPath_id);
		int num = (int)tempMap.get("num")-list.size();
		Map<String, Object>map = new HashMap<String,Object>();
		map.put("num", num);
		map.put("list", list);
		return map;
	}
	
	@RequestMapping(value = "/panelpoint/delete_panelpoint")
	@ResponseBody
	public int deletePanelPoint(PanelPoint panelPoint,HttpServletRequest request,HttpServletResponse response){
		return service.deletePanelPoint(panelPoint);
	}
	
	@RequestMapping(value = "/panelpoint/update_panelpoint")
	@ResponseBody
	public BaseResult1 updatePanelPoint(PanelPoint panelPoint,HttpServletRequest request,HttpServletResponse response){
		BaseResult1 result = new BaseResult1();
		PanelPoint oldPoint = service.queryPanelPointByPanelId(panelPoint.getPanelpoint_id());
		boolean flag = panelPoint.getName().equals(oldPoint.getName())&panelPoint.getWarn().equals(oldPoint.getWarn());
		boolean flag1 = panelPoint.getSequence() == oldPoint.getSequence();
		boolean flag2 = panelPoint.getOperate() == oldPoint.getOperate();
		
		if (flag&flag1&flag2) {
			result.setRet(BaseResult1.BIZ_ERROR);
			result.setMsg("未修改不能进行修改操作");
			RestUtils.error(response, result);
			return result;
		}
		if (StringUtils.isBlank(panelPoint.getName())) {
			result.setRet(BaseResult1.BIZ_ERROR);
			result.setMsg("节点名称不能为空");
			RestUtils.error(response, result);
			return result;
		}
		if ("".equals(panelPoint.getOperate())) {
			result.setRet(BaseResult1.BIZ_ERROR);
			result.setMsg("双录节点操作不能为空");
			RestUtils.error(response, result);
			return result;
		}
		if (StringUtils.isBlank(panelPoint.getWarn())) {
			result.setRet(BaseResult1.BIZ_ERROR);
			result.setMsg("双录提示语不能为空");
			RestUtils.error(response, result);
			return result;
		}
		 UserSessionBo user = (UserSessionBo) request.getSession().getAttribute("user");
		 panelPoint.setEditUser(user.getOptName());
		int num = service.updatepanelPoint(panelPoint);
		if (num >0) {
			result.setRet(BaseResult1.SUCCESS);
			result.setMsg("修改成功");
			return result;
		}else{
			result.setRet(BaseResult1.SUCCESS);
			result.setMsg("修改失败");
			return result;
		}
	}
	
}
